var all_divs = [
"login_div","registration_div","tcon-main-dashboard","pomain_div",'createpo_table_container','podetails_div','comp_mgmt_div','supp_mgmt_div','user_mgmt_div','client_mgmt_div','registration_div_edit',
'createpoinvoice_table_container','createnonpoinvoice_table_container','createcreditmemo_table_container','paymentdetails_table_container','invdetails_div','pmtdetails_div',
'cmemodetails_div','mainSearch_div','openDocument_div','invmain_div','cmmain_div','paymain_div'
];

var page_map =  null;
var curr_page = null;
var curr_subpage = null;

function exception_generic_handler(exceptionMessage, exception){
	alert(exceptionMessage);
}

function u_safeSetText_ChangeClass(el_id, text, cssclass){
	var e = document.getElementById(el_id);
	if(e){
		e.setAttribute('class',cssclass);
	}
	e = null;
	dwr.util.setValue(el_id,text);
}

function u_safeShowElement(el_id){
	var e = document.getElementById(el_id);
	if(e){
		e.style.display = 'block';
	}
	e = null;
}

function u_safeHideElement(el_id){
	var e = document.getElementById(el_id);
	if(e){
		e.style.display = 'none';
	}
	e = null;
}

function u_hideAllSections(){
	var i=0;
	for(i=0;i<all_divs.length;i++){
		u_safeHideElement(all_divs[i]);
	}
	i=null;
}

function u_safe_setPageHeader(headertext){
	dwr.util.setValue('tn_page_Header', headertext);
}

function trimString(str){
	if(str){
		str = str.replace(/^\s+|\s+$/g, '') ;
	}
	return str;
}


function isEmptyString(s) {
	if (typeof s == "undefined") {
		return true;
	}

	if (s == null) {
		return true;
	}
	if (s == "") {
		return true;
	}
	if (s == " ") {
		return true;
	}
	if (s == "&nbsp;" || s == "&NBSP;" ) {
		return true;
	}

	return false;
}

function isEmptyField(el_id){
	var value = dwr.util.getValue(el_id);
	return isEmptyString(value);
}


function tn_takeToPage(page_id){
	
	u_hideAllSections();
	var page_obj = page_map[page_id];
	if(page_obj && page_obj != null){
		u_safeShowElement(page_obj.container_div);
		u_safe_setPageHeader(page_obj.header);
	}
	div_id = null;
}

function tn_onMainMenuClick(e,loc,args){
	u_hideAllSections();
	var page_id = args[0];
	var subpage_id = args[1];
	var page_obj = page_map[page_id];
	curr_page = page_obj;
	if(page_obj && page_obj != null){
		u_safeShowElement(page_obj.container_div);
		var headerStr = page_obj.header;
		if(page_obj.subpages != null){
			var i=0;
			for(i=0;i<page_obj.subpages.length;i++){
				if(page_obj.subpages[i].container_div != subpage_id){
						u_safeHideElement(page_obj.subpages[i].container_div);
				}else{
					u_safeShowElement(page_obj.subpages[i].container_div);
					curr_subpage = page_obj.subpages[i];
					headerStr += " - " + page_obj.subpages[i].header;
					//if the current subpage has more subpages, show the first by default.
					if(curr_subpage.subpages != null && curr_subpage.subpages.length > 0){
						u_safeShowElement(page_obj.subpages[i].subpages[0].container_div);
						for(q=1;q<curr_subpage.subpages.length;q++){
							u_safeHideElement(curr_subpage.subpages[q].container_div);
						}
					}
					if(!isEmptyString(page_obj.subpages[i].initfunc)){
						try { var func = window[page_obj.subpages[i].initfunc]; 
								func();
							} catch(ex) {
								console.log(ex);
							}
					}
				}
			}
			i=null;
		}
		
		u_safe_setPageHeader(headerStr);
	}
	div_id = null;
}

function tn_takeToSubSection(e,loc,subsection_id){
	if(curr_subpage != null){
			if(curr_subpage.subpages != null && curr_subpage.subpages.length > 0){
				var i=0;
				for(i=0;i<curr_subpage.subpages.length;i++){
					if(curr_subpage.subpages[i].container_div != subsection_id){
						u_safeHideElement(curr_subpage.subpages[i].container_div);
					}else{
						u_safeShowElement(curr_subpage.subpages[i].container_div);
						if(!isEmptyString(curr_subpage.subpages[i].initfunc)){
							try { var func = window[curr_subpage.subpages[i].initfunc]; 
									func();
									func = null;
								} catch(ex) {
									console.log(ex);
								}
						}
					}
				}
					
			}
	}
}



function tn_onRecdMenu1Click(e,loc,args){
	tn_takeToSubSection(e,loc,args[0]);
}

function format_date(d){
	var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	var curr_date = d.getDate();
	var curr_month = d.getMonth() + 1;
	var curr_year = d.getFullYear();
	//return curr_date + "-" + m_names[curr_month]  + "-" + curr_year;
	return (curr_month < 10 ? "0" + curr_month + "" : curr_month) + "/" + (curr_date < 10? "0" + curr_date + "" : curr_date) + "/" + curr_year;
}

function sortByPONum(a, b, desc) {
    // Deal with empty values
    if(!YAHOO.lang.isValue(a)) {
        return (!YAHOO.lang.isValue(b)) ? 0 : 1;
    }
    else if(!YAHOO.lang.isValue(b)) {
        return -1;
    }

    // First compare by state
    var comp = YAHOO.util.Sort.compare;
    var a1 = Number(a.getData("orderNumber").replace("PO",""));
    var b1 = Number(b.getData("orderNumber").replace("PO",""));
    var compState = comp(a1, b1, desc);

    return compState;
}

function turnThrobberOn(message){
	document.getElementById('showprogress_div').style.visibility = 'visible';
	document.getElementById('progress_text').innerHTML = message + "...";
	document.getElementById('showprogress_throbber').className = "throbberOn";
}

function turnThrobberOff(){
	document.getElementById('showprogress_div').style.visibility = 'visible';
	document.getElementById('progress_text').innerHTML = "";
	document.getElementById('showprogress_throbber').className = "throbberOff";
}

function formatXml(xml) {
	var formatted = '';
	var reg = /(>)(<)(\/*)/g;
	xml = xml.replace(reg, '$1\r\n$2$3');
	var pad = 0;
	var nodes = xml.split('\r\n');
	for ( var index = 0; index < nodes.length; index++) {
		var node = nodes[index];
		var indent = 0;
		if (node.match(/.+__tag_66$27_]*>$/)) {
			indent = 0;
		} else if (node.match(/^<\/\w/)) {
			if (pad != 0) {
				pad -= 1;
			}
		} else if (node.match(/^__tag_72$33_]*[^\/]>.*$/)) {
			indent = 1;
		} else {
			indent = 0;
		}

		var padding = '';
		for ( var i = 0; i < pad; i++) {
			padding += '  ';
		}

		formatted += padding + node + '\r\n';
		pad += indent;
	}
	return formatted;
}

var xmldata_dlg = null;

function showxmldata(data){
	
	dwr.util.setValue('xmldatatoshow','');
	if(isEmptyString(data)){
		dwr.util.setValue('xmldatatoshow','No XML Data to show.');
	}else{
		var fdata = formatXml(data);
		dwr.util.setValue('xmldatatoshow',fdata,{escapeHtml: false});
	}
	
	if(xmldata_dlg == null){
		var handleYes = function() {
		    //user confirms the deletion of this item;
		    //this method would perform that deletion;
		    //when ready, hide the SimpleDialog:
		    this.hide();
		};
		var myButtons = [
		    { text: "Close", handler: handleYes }
		];
		xmldata_dlg = new YAHOO.widget.SimpleDialog("xmldatacontainer",
				{
					width : "500px",
					fixedcenter : true,
					modal : true,
					visible : false,
					draggable : true,
					close : true,
					icon : YAHOO.widget.SimpleDialog.ICON_INFO,
					constraintoviewport : true
				});
		xmldata_dlg.setHeader("XML Data");
		xmldata_dlg.cfg.queueProperty("buttons", myButtons);
		xmldata_dlg.render(document.body);
		
	}
	xmldata_dlg.show();
	document.getElementById('xmldatacontainer').style.display = 'block';
}

function openAttachmentPanel(attachmentURL){
	//window.open(attachmentURL,'attachment','top=200,width=400,height=200,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no'); 
}